<?php

namespace App\Http\Controllers\Personal;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Personal\Interview;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Style;

class InterviewController extends CommonController{
    /**
     * 面试通知
     * @param Request $request
     * @return mixed
     */
    public function interview(Request $request){
        $user = auth()->guard('personal')->user();
        //关键词搜索
        $keyword  = trim($request->keyword);
        //获取面试通知记录
        $interview = DB::table('interview as t1')
                    ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
                    ->join('company_job as t3', 't1.job_id', '=', 't3.id')
                    ->select(
                        't1.interview_time','t1.id','t1.status','t1.com_id','t1.job_id',
                        't2.company_name','t2.region',
                        't3.job_title','t3.salary_above','t3.salary_below'
                    )
                    ->where('t1.personal_uid',$user->uid)
                    ->where('t1.status','!=',4)
                    ->where(function ($query) use($keyword) {
                        if(strlen($keyword) > 0){
                            $query->where('t3.job_title', 'like', '%'.$keyword.'%')
                                ->orWhere('t2.company_name','like','%'.$keyword.'%');
                        }
                    })
                    ->orderBy('updated_at', 'desc')
                    ->paginate(10);
        //处理数组
        foreach($interview as $v){
            $v->company_name = str_limit($v->company_name,24);
            $v->job_title = str_limit($v->job_title,24);
            $v->region = str_limit(str_replace(',','',CollArea::getCityAddress($v->region)),18);
            $v->salary_above = ($v->salary_above) ? CollOption::getOptionName($v->salary_above,'expectedsalary') : '';
            $v->salary_below = ($v->salary_below) ? CollOption::getOptionName($v->salary_below,'expectedsalary') : '';
            $v->interview_time = substr($v->interview_time,0,10);
        }
        //分页分配参数 过滤boolean假值
        $appends =arrayFilter(array(
            'keyword' => $keyword
        ));
        return Style::view('personal.interview',compact('interview','appends'));
    }

    /**
     * 面试状态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function interviewStatus(Request $request){
        $user = auth()->guard('personal')->user();
        $input['status'] = $request->status;
        $input['updated_at'] = date('Y-m-d H:i:s',time());
        $result =  Interview::where('id',$request->id)->where('personal_uid', $user->uid)->update($input);
        if($result){
            return response()->json(['status' => 0,'msg' => '更新面试状态成功！']);
        }else{
            return response()->json(['status' => 1,'msg' => '更新面试状态失败，请稍后重试！']);
        }
    }

    /**
     * 面试删除
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function interviewDel(Request $request){
        $user = auth()->guard('personal')->user();
        $selected_id = $request->input('selected_id.*');
        $input['status'] = 4;
        $input['updated_at'] = date('Y-m-d H:i:s',time());
        foreach($selected_id as $v){
            Interview::where('id',$v)->where('personal_uid', $user->uid)->update($input);
        }
        return redirect('personal/interview')->with('ok','删除面试记录成功');
    }
}

